/*
 * @Author: 0x9DEFA478
 * @Date: 2022-02-14 22:08:03
 * @LastEditTime: 2022-08-24 21:40:21
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "../H_NES_Mapper.h"




extern void H_NES_Mapper67_PRG_Write(Hbyte Value,Huint16 Address,void* v);
extern Hbyte H_NES_Mapper67_PRG_Read(Huint16 Address,void* v);
extern Huint16 H_NES_Mapper67_PRG_Read16(Huint16 Address,void* v);
extern void H_NES_Mapper67_CHR_Exception_Write(Hbyte Value,Huint16 Address,void* v);
extern void H_NES_Mapper67_CHR_Write(Hbyte Value,Huint16 Address,void* v);
extern Hbyte H_NES_Mapper67_CHR_Read(Huint16 Address,void* v);
extern void H_NES_Mapper67_CPUTick(int CPUCycle,void* v);




#define ThisMapper Cartridge.Mapper.SpecificMapper.Mapper67




void H_NES_Mapper67_MapperUpdate(void* v){
  H_NES_Console* Console;
  Hbyte Reg;
  Hbyte* p;
  Huint32 ROM_Size;
  Hbyte Msk;

  Console=v;

  ROM_Size=Console->Cartridge.Mapper.PRG_ROM_Size;
  p=Console->Cartridge.Mapper.PRG_ROM;

  Reg=Console->ThisMapper.Reg_F800;
  Console->ThisMapper.PRG_Banks.Base8000_16KB[0]=&p[(((Huint32)Reg&0x1FU)<<14)%ROM_Size];


  ROM_Size=Console->Cartridge.Mapper.CHR_ROM_Size;
  if(ROM_Size==0){
    ROM_Size=0x2000U;
    Msk=0x3FU;
  }else{
    if(ROM_Size>128U*1024U){
      Msk=0xFFU;
    }else{
      Msk=0x3FU;
    }
  }
  p=Console->Cartridge.Mapper.CHR_ROM;

  Reg=Console->ThisMapper.Reg_8800;
  Console->ThisMapper.CHR_Banks.Base0000_2KB[0]=&p[(((Huint32)Reg&Msk)<<11)%ROM_Size];
  Reg=Console->ThisMapper.Reg_9800;
  Console->ThisMapper.CHR_Banks.Base0000_2KB[1]=&p[(((Huint32)Reg&Msk)<<11)%ROM_Size];
  Reg=Console->ThisMapper.Reg_A800;
  Console->ThisMapper.CHR_Banks.Base0000_2KB[2]=&p[(((Huint32)Reg&Msk)<<11)%ROM_Size];
  Reg=Console->ThisMapper.Reg_B800;
  Console->ThisMapper.CHR_Banks.Base0000_2KB[3]=&p[(((Huint32)Reg&Msk)<<11)%ROM_Size];

}

void H_NES_Mapper67_PRG_Write_Ext(Hbyte Value,Huint16 Address,H_NES_Console* Console){

  if((Address&0x8800U)==0x8000U){
    Console->CPU.Status&=(~vH_NES_CPU_Status_IRQ_ExtIRQ);
  }else{
    switch(Address&0xF800U){
      case 0x8800U:
        Console->ThisMapper.Reg_8800=Value;
        H_NES_Mapper67_MapperUpdate(Console);
        break;
      case 0x9800U:
        Console->ThisMapper.Reg_9800=Value;
        H_NES_Mapper67_MapperUpdate(Console);
        break;
      case 0xA800U:
        Console->ThisMapper.Reg_A800=Value;
        H_NES_Mapper67_MapperUpdate(Console);
        break;
      case 0xB800U:
        Console->ThisMapper.Reg_B800=Value;
        H_NES_Mapper67_MapperUpdate(Console);
        break;
      case 0xC800U:
        if(Console->ThisMapper.CountWriteCount==0x00U){
          Console->ThisMapper.CountWriteCount=0xFFU;
          Console->ThisMapper.Count=(Console->ThisMapper.Count&0x00FFU)|(((Huint16)Value)<<8);
        }else{
          Console->ThisMapper.CountWriteCount=0x00U;
          Console->ThisMapper.Count=(Console->ThisMapper.Count&0xFF00U)|((Huint16)Value);
        }
        break;
      case 0xD800U:
        Console->ThisMapper.Reg_D800=Value;
        Console->ThisMapper.CountWriteCount=0x00U;
        break;
      case 0xE800U:
        if(Console->PPU.VRAM_ScreenType!=0x03U){
          switch(Value&0x03U){
            case 0x0U:
              //命名表水平排列
              Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
              Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0x400];
              Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0];
              Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
              Console->PPU.VRAM_ScreenType=0x02U;
              break;
            case 0x1U:
              //命名表垂直排列
              Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
              Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0];
              Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0x400];
              Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
              Console->PPU.VRAM_ScreenType=0x01U;
              break;
            case 0x2U:
              Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
              Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0];
              Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0];
              Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0];
              Console->PPU.VRAM_ScreenType=0x00U;
              break;
            case 0x3U:
              Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0x400];
              Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0x400];
              Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0x400];
              Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
              Console->PPU.VRAM_ScreenType=0x00U;
              break;
            default:
              break;
          }
        }

        break;
      case 0xF800U:
        Console->ThisMapper.Reg_F800=Value;
        H_NES_Mapper67_MapperUpdate(Console);
        break;
      default:
        break;
    }
  }
  
  
}




static void GetProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultGetProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  p[0]=Console->ThisMapper.Reg_8800;
  p[1]=Console->ThisMapper.Reg_9800;
  p[2]=Console->ThisMapper.Reg_A800;
  p[3]=Console->ThisMapper.Reg_B800;
  p[4]=Console->ThisMapper.Reg_D800;
  p[5]=Console->ThisMapper.Reg_F800;

  p[6]=((Hbyte*)&Console->ThisMapper.Count)[0];
  p[7]=((Hbyte*)&Console->ThisMapper.Count)[1];

  p[8]=Console->ThisMapper.CountWriteCount;

}

static void LoadProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultLoadProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  Console->ThisMapper.Reg_8800=p[0];
  Console->ThisMapper.Reg_9800=p[1];
  Console->ThisMapper.Reg_A800=p[2];
  Console->ThisMapper.Reg_B800=p[3];
  Console->ThisMapper.Reg_D800=p[4];
  Console->ThisMapper.Reg_F800=p[5];

  ((Hbyte*)&Console->ThisMapper.Count)[0]=p[6];
  ((Hbyte*)&Console->ThisMapper.Count)[1]=p[7];

  Console->ThisMapper.CountWriteCount=p[8];
  
  H_NES_Mapper67_MapperUpdate(Console);
}

int H_NES_Mapper67_Init(void* v){
  H_NES_Console* Console;

  Console=v;


  if(Console->Cartridge.Mapper.PRG_RAM_Size<0){
    Console->Cartridge.Mapper.PRG_RAM_Size=0x2000;
    Console->Cartridge.Mapper.PRG_RAM=Console->Memory.Malloc(0x2000);
    if(Console->Cartridge.Mapper.PRG_RAM==NULL){
      return vH_NES_Result_PRG_RAM_AllocFail;
    }
  }


  Console->ThisMapper.Reg_8800=0x00U;
  Console->ThisMapper.Reg_9800=0x00U;
  Console->ThisMapper.Reg_A800=0x00U;
  Console->ThisMapper.Reg_B800=0x00U;
  Console->ThisMapper.Reg_D800=0x00U;
  Console->ThisMapper.Reg_F800=0x00U;
  Console->ThisMapper.Count=0U;
  Console->ThisMapper.CountWriteCount=0x00U;

  Console->ThisMapper.PRG_Banks.Base8000_16KB[1]=&Console->Cartridge.Mapper.PRG_ROM[Console->Cartridge.Mapper.PRG_ROM_Size-0x4000U];

  H_NES_Mapper67_MapperUpdate(Console);


  Console->Cartridge.Mapper.DeInit=NULL;

  
  Console->Cartridge.PRG_Write=H_NES_Mapper67_PRG_Write;
  Console->Cartridge.PRG_Read=H_NES_Mapper67_PRG_Read;
  Console->Cartridge.PRG_Read16=H_NES_Mapper67_PRG_Read16;
  
  if(Console->Cartridge.Mapper.CHR_ROM_Size==0U){
    Console->Cartridge.CHR_Write=H_NES_Mapper67_CHR_Write;
  }else{
    Console->Cartridge.CHR_Write=H_NES_Mapper67_CHR_Exception_Write;
  }
  Console->Cartridge.CHR_Read=H_NES_Mapper67_CHR_Read;

  Console->Cartridge.Progress.Size=9+H_NES_Mapper_DefaultProgressSize(Console);
  Console->Cartridge.Progress.Get=GetProgress;
  Console->Cartridge.Progress.Load=LoadProgress;

  Console->Cartridge.Option.CPUTick=H_NES_Mapper67_CPUTick;
  
  


  return vH_NES_Result_OK;
}




#undef ThisMapper



